﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing;

namespace CamShared
{
    /*
    public class SettingsDEPRECATED
    {
        public delegate void SettingChanged(ConfValue c);
        public static event SettingChanged OnSettingChanged;

        static string SavePath = Environment.GetFolderPath(
            Environment.SpecialFolder.LocalApplicationData) + "\\CamServer_conf.xml";

        //static string SavePath = System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + "conf.xml";

        public static List<ConfValue> Dict = new List<ConfValue>();

        public static Rectangle Get(string key, Rectangle sDefault)
        {
            string r = Get(key, "");
            if (r != "")
            {
                if (r.Contains('|'))
                {
                    try
                    {
                        string[] parts = r.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                        if (parts.Length == 4)
                        {
                            return new Rectangle(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3]));
                        }
                    }
                    catch (Exception) { }
                }
            }
            return sDefault;
        }

        public static bool Get(string key, bool sDefault)
        {
            return Get(key, sDefault ? "YES" : "NO", null, true) == "YES";
        }

        public static int Get(string key, int sDefault)
        {
            try
            {
                return int.Parse(Get(key, sDefault.ToString(), null, true));
            }
            catch
            {
                return sDefault;
            }
        }

        public static string Get(string key, string sDefault)
        {
            return Get(key, sDefault, sDefault, true);
        }

        public static void SetRestartRequired(string key)
        {
            SetRestartRequired(key, true);
        }

        public static void SetRestartRequired(string key, bool restartIsRequired)
        {
            for (int i = 0; i < Dict.Count; i++)
            {
                ConfValue KV = Dict[i];

                if (KV.Key == key)
                {
                    KV.RestartIsRequired = true;
                    if (OnSettingChanged != null)
                    {
                        OnSettingChanged.Invoke(KV);
                    }
                    return;
                }
            }
        }

        public static void Set(string key, Rectangle value)
        {
            Set(key, value.X + "|" + value.Y + "|" + value.Width + "|" + value.Height);
        }

        public static void Set(string key, bool value)
        {
            Set(key, value ? "YES" : "NO");
        }

        public static void Set(string key, int value)
        {
            Set(key, value.ToString());
        }

        public static void Set(string key, string value)
        {
            for (int i = 0; i < Dict.Count; i++)
            {
                ConfValue KV = Dict[i];

                if (KV.Key == key)
                {
                    KV.Value = value;

                    if (key == "#Settings|Save On Modification" || Settings.Get("#Settings|Save On Modification", true))
                    {
                        Save();
                    }
                    if (OnSettingChanged != null)
                    {
                        OnSettingChanged.Invoke(KV);
                    }
                    return;
                }
            }

                ConfValue c = new ConfValue(key, value);
                Dict.Add(c);

                if (OnSettingChanged != null)
                {
                    OnSettingChanged.Invoke(c);
                }

                if (key == "#Settings|Save On Modification" || Settings.Get("#Settings|Save On Modification", true))
                {
                    Save();
                }
        }


        public static string Get(string key, string sDefault, string error, bool create)
        {
            for(int i = 0; i < Dict.Count; i++)
            {
                ConfValue KV = Dict[i];

                if (KV.Key == key)
                {
                    return KV.Value;
                }
            }
            if (create)
            {
                ConfValue c = new ConfValue(key, sDefault);
                Dict.Add(c);

                if (OnSettingChanged != null)
                {
                    OnSettingChanged.Invoke(c);
                }

                if (key == "#Settings|Save On Modification" || Settings.Get("#Settings|Save On Modification", true))
                {
                    Save();
                }
                return sDefault;
            }
            return error;
        }
        
        
        static SettingsDEPRCATED()
        {
            try
            {
                Dict = DavuxLib.XSerializer.Load<List<ConfValue>>(SavePath);
            }
            catch(Exception ex) 
            {
                Trace.WriteLine("Error Loading Settings: " + ex.Message);
            }

            
            if (Get("#ConfigVersion", "1.1") != "1.1")
            {
                Trace.WriteLine("Settings Upgrade!");
                
            }
            
        }

        public static void Save()
        {
            try
            {
                DavuxLib.XSerializer.Save<List<ConfValue>>(SavePath, Dict);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error Saving Settings: " + ex.Message);
            }
        }


    }

    public class ConfValue
    {
        public ConfValue(string key, string value)
        {
            Key = key; Value = value;
        }
        public ConfValue() { }
        public string Key { get; set; }
        public string Value { get; set; }
        [XmlIgnore]
        public bool RestartRequired { get; set; }
        [XmlIgnore]
        public bool RestartIsRequired { get; set; }
    }
    */
}
